<template>
  <div class="app-container">
    <!-- ====================== 查询条件 ====================== -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="IMEI" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入IMEI"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="警报名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入警报名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="警报值" prop="value">
        <el-input
          v-model="queryParams.value"
          placeholder="请输入警报值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="警报时间" prop="timeStamp">
        <el-input
          v-model="queryParams.timeStamp"
          placeholder="请输入警报时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态: 0：未读；1：已读" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态: 0：未读；1：已读"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- ====================== 工具栏 ====================== -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:alarm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:alarm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:alarm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:alarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- ====================== 列表 ====================== -->
    <el-table v-loading="loading" :data="alarmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="IMEI" align="center" prop="sn" />
      <el-table-column label="警报名称" align="center" prop="name" />
      <el-table-column label="警报值" align="center" prop="value" />
      <el-table-column label="警报时间" align="center" prop="timeStamp" />
      <el-table-column label="状态: 0：未读；1：已读" align="center" prop="state" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:alarm:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:alarm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- ====================== 新增 / 修改 对话框 ====================== -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="IMEI" prop="sn">
          <!-- 当编辑时，不允许修改 sn -->
          <el-input v-model="form.sn" placeholder="请输入IMEI" :disabled="!isAdd" />
        </el-form-item>
        <el-form-item label="警报名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入警报名称" />
        </el-form-item>
        <el-form-item label="警报值" prop="value">
          <el-input v-model="form.value" placeholder="请输入警报值" />
        </el-form-item>
        <el-form-item label="警报时间" prop="timeStamp">
          <el-input v-model="form.timeStamp" placeholder="请输入警报时间" />
        </el-form-item>
        <el-form-item label="状态: 0：未读；1：已读" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态: 0：未读；1：已读" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAlarm, getAlarm, delAlarm, addAlarm, updateAlarm } from "@/api/system/alarm";

export default {
  name: "Alarm",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 列表数据
      alarmList: [],
      // 弹窗标题
      title: "",
      // 弹窗开关
      open: false,
      // 当前是否是新增模式
      isAdd: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sn: null,
        name: null,
        value: null,
        timeStamp: null,
        state: null
      },
      // 表单数据
      form: {},
      // 校验规则
      rules: {
        sn: [
          { required: true, message: "IMEI不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listAlarm(this.queryParams).then(response => {
        this.alarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置表单
    reset() {
      this.form = {
        sn: null,
        name: null,
        value: null,
        timeStamp: null,
        state: null
      };
      this.$nextTick(() => {
        this.resetForm("form");
      });
    },
    /** 搜索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置查询 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 复选框变化
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sn);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增 */
    handleAdd() {
      this.reset();
      this.isAdd = true;
      this.open = true;
      this.title = "添加警报";
    },
    /** 修改 */
    handleUpdate(row) {
      this.reset();
      const sn = row.sn || this.ids;
      getAlarm(sn).then(response => {
        this.form = response.data;
        this.isAdd = false;
        this.open = true;
        this.title = "修改警报";
      });
    },
    /** 新增 / 修改 提交 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) return;
        if (this.isAdd) {
          // 新增逻辑
          addAlarm(this.form).then(() => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        } else {
          // 修改逻辑
          updateAlarm(this.form).then(() => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除 */
    handleDelete(row) {
      const sns = row.sn || this.ids;
      this.$modal
        .confirm('是否确认删除警报编号为"' + sns + '"的数据项？')
        .then(() => delAlarm(sns))
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出 */
    handleExport() {
      this.download('system/alarm/export', {
        ...this.queryParams
      }, `alarm_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
